package com.hxkj.cst.chengshuitong.ui.activity

import android.os.Build
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.view.WindowManager
import com.hxkj.cst.chengshuitong.R
import com.hxkj.cst.chengshuitong.closeInputMethod
import com.hxkj.cst.chengshuitong.control.RegisterControl
import com.hxkj.cst.chengshuitong.model.ServiceResultModel
import com.hxkj.cst.chengshuitong.presenter.RegisterPresenter
import com.hxkj.cst.chengshuitong.toast
import kotlinx.android.synthetic.main.activity_register.*

/**
 * 作者： Luoyang on 2017/5/27.
 * 描述：
 */
class RegisterActivity : AppCompatActivity(), RegisterControl.View {
    var mRegisterPresenter: RegisterPresenter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)
        if (mRegisterPresenter == null)
            mRegisterPresenter = RegisterPresenter(this, this)
        setUpToolbar()
        setListener()
    }

    fun setUpToolbar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        }

        setSupportActionBar(ar_toolbar)

        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setDisplayShowTitleEnabled(false)

        ar_tv_title.text = getString(R.string.title_register)
    }



    fun setListener() {
        ar_btn_register.setOnClickListener { register() }
        ar_ci_code.getAction().setOnClickListener {
            AlertDialog.Builder(this)
                    .setTitle("验证码")
                    .setMessage("您当前的验证码为12345")
                    .setCancelable(false)
                    .setPositiveButton("确定", { dialog, _ -> dialog.dismiss() })
                    .show()
        }
    }

    fun register() {
        closeInputMethod(ar_btn_register.windowToken)

        val phone = ar_ci_phone.getText()
        val code = ar_ci_code.getText()
        val password = ar_ci_password.getText()
        val email = ar_ci_email.getText()

        if (TextUtils.isEmpty(phone))
            toast("手机号不能为空")
        else if (TextUtils.isEmpty(password))
            toast("密码不能为空")
        else if (TextUtils.isEmpty(code))
            toast("验证码不能为空")
        else if (TextUtils.isEmpty(email))
            toast("邮箱不能为空")
        else if (code != "12345")
            toast("验证码输入错误请重新输入")
        else mRegisterPresenter?.register(phone, password, email)

    }

    override fun registerResult(result: String) {
        if (result == ServiceResultModel.SUCCESS)
            toast("注册成功")
        finish()
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return super.onSupportNavigateUp()
    }

}